

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>ccqw.progressDialog &mdash; CCQW 0.1.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/sidebar.js"></script>
    <link rel="top" title="CCQW 0.1.1 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for ccqw.progressDialog</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="sd">&quot;&quot;&quot; A Progress Dialog module</span>

<span class="sd">    :Author:</span>
<span class="sd">      - 20111202-20120110 Roberto Vidmar</span>

<span class="sd">    :Revision:  $Revision: 52 $</span>
<span class="sd">                $Date: 2012-11-12 10:07:15 +0100 (Mon, 12 Nov 2012) $</span>

<span class="sd">    :Copyright: 2011-2012</span>
<span class="sd">                Nicola Creati &lt;ncreati@inogs.it&gt;</span>
<span class="sd">                Roberto Vidmar &lt;rvidmar@inogs.it&gt;</span>

<span class="sd">    :License: MIT/X11 License (see :download:`license.txt</span>
<span class="sd">                               &lt;../../license.txt&gt;`)</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">sys</span>
<span class="c">#import time</span>
<span class="kn">from</span> <span class="nn">qtCompat</span> <span class="kn">import</span> <span class="n">QtCore</span><span class="p">,</span> <span class="n">QtGui</span><span class="p">,</span> <span class="n">Signal</span>

<span class="c">#===============================================================================</span>
<div class="viewcode-block" id="ProgressDialog"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ProgressDialog">[docs]</a><span class="k">class</span> <span class="nc">ProgressDialog</span><span class="p">(</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QDialog</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Processing gui with progress bar and message</span>
<span class="sd">  &quot;&quot;&quot;</span>

  <span class="c"># Custom signal that notifies abort of the process</span>
  <span class="n">abortSignal</span> <span class="o">=</span> <span class="n">Signal</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>

<div class="viewcode-block" id="ProgressDialog.__init__"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ProgressDialog.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tf</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s">&#39;Processor Progress Dialog&#39;</span><span class="p">,</span>
    <span class="n">msg</span><span class="o">=</span><span class="s">&#39;The Message.&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Create a new ProgressDialog instance.</span>

<span class="sd">      :param tf: a :class:`ThreadedFunc` instance</span>
<span class="sd">      :type tf: :class:`ThreadedFunc`</span>
<span class="sd">      :param parent: parent widget</span>
<span class="sd">      :type parent: QtGui widget</span>
<span class="sd">      :param title: title for the progress dialog</span>
<span class="sd">      :type title: string, unicode</span>
<span class="sd">      :param msg: progress message</span>
<span class="sd">      :type msg: string, unicode</span>
<span class="sd">      :param value: initial value</span>
<span class="sd">      :type value: int</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nb">super</span><span class="p">(</span><span class="n">ProgressDialog</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">setWindowTitle</span><span class="p">(</span><span class="n">title</span><span class="p">)</span>

    <span class="c"># Set the minimu width</span>
    <span class="c">#self.setMinimumWidth(320)</span>

    <span class="c"># Main layout</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QVBoxLayout</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="c"># Standard progress bar</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_msg</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QLabel</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_bar</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QProgressBar</span><span class="p">()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_bar</span><span class="o">.</span><span class="n">setValue</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

    <span class="c"># Dialog button with cancel to stop the thread</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_buttonBox</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QDialogButtonBox</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_buttonBox</span><span class="o">.</span><span class="n">setObjectName</span><span class="p">(</span><span class="s">&quot;buttonBox&quot;</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_buttonBox</span><span class="o">.</span><span class="n">setStandardButtons</span><span class="p">(</span><span class="n">QtGui</span><span class="o">.</span><span class="n">QDialogButtonBox</span><span class="o">.</span><span class="n">Abort</span><span class="p">)</span>

    <span class="c"># Final layout</span>
    <span class="n">l</span><span class="o">.</span><span class="n">addWidget</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_msg</span><span class="p">)</span>
    <span class="n">l</span><span class="o">.</span><span class="n">addWidget</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_bar</span><span class="p">)</span>
    <span class="n">l</span><span class="o">.</span><span class="n">addWidget</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_buttonBox</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">_buttonBox</span><span class="o">.</span><span class="n">clicked</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">abort</span><span class="p">)</span>
    <span class="n">tf</span><span class="o">.</span><span class="n">progressSignal</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">progress</span><span class="p">)</span>
    <span class="n">tf</span><span class="o">.</span><span class="n">closeSignal</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">onClose</span><span class="p">)</span>

    <span class="c"># Show the widget</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setMinimumSize</span><span class="p">(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QSize</span><span class="p">(</span><span class="mi">300</span><span class="p">,</span> <span class="mi">100</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">exec_</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="ProgressDialog.showMessage"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ProgressDialog.showMessage">[docs]</a>  <span class="k">def</span> <span class="nf">showMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">flag</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Show/Hide message</span>

<span class="sd">      :param flag: Show/Hide message</span>
<span class="sd">      :type flag: bool</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_msg</span><span class="o">.</span><span class="n">setVisible</span><span class="p">(</span><span class="n">flag</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="ProgressDialog.progress"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ProgressDialog.progress">[docs]</a>  <span class="k">def</span> <span class="nf">progress</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Update progress bar</span>

<span class="sd">      :param value: progress value</span>
<span class="sd">      :type value: int [0, 100]</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_bar</span><span class="o">.</span><span class="n">setValue</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="ProgressDialog.setMessage"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ProgressDialog.setMessage">[docs]</a>  <span class="k">def</span> <span class="nf">setMessage</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Set text message</span>

<span class="sd">      :param msg: text message</span>
<span class="sd">      :type msg: string, unicode</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_msg</span><span class="o">.</span><span class="n">setText</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="ProgressDialog.onClose"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ProgressDialog.onClose">[docs]</a>  <span class="k">def</span> <span class="nf">onClose</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="c">#===============================================================================</span></div></div>
<div class="viewcode-block" id="ThreadedFunc"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ThreadedFunc">[docs]</a><span class="k">class</span> <span class="nc">ThreadedFunc</span><span class="p">(</span><span class="n">QtCore</span><span class="o">.</span><span class="n">QThread</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; A class to run a function inside a Thread.</span>
<span class="sd">  &quot;&quot;&quot;</span>
  <span class="n">progressSignal</span> <span class="o">=</span> <span class="n">Signal</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>
  <span class="n">endedSignal</span> <span class="o">=</span> <span class="n">Signal</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>
  <span class="n">closeSignal</span> <span class="o">=</span> <span class="n">Signal</span><span class="p">()</span>

<div class="viewcode-block" id="ThreadedFunc.__init__"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ThreadedFunc.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Create a new ThreadedFunc instance.</span>

<span class="sd">        \*args and \**kargs are fed to `func`</span>

<span class="sd">      :param func: the function to run</span>
<span class="sd">      :type func: function</span>
<span class="sd">      :param \*args: various</span>
<span class="sd">      :type \*args: various</span>
<span class="sd">      :param \*kargs: various</span>
<span class="sd">      :type \*kargs: various</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">ThreadedFunc</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_func</span> <span class="o">=</span> <span class="n">func</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_quit</span> <span class="o">=</span> <span class="bp">False</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_args</span> <span class="o">=</span> <span class="n">args</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_kargs</span> <span class="o">=</span> <span class="n">kargs</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</div>
<div class="viewcode-block" id="ThreadedFunc.abort"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ThreadedFunc.abort">[docs]</a>  <span class="k">def</span> <span class="nf">abort</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Set the &quot;we want abort&quot; flag</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_quit</span> <span class="o">=</span> <span class="bp">True</span>
</div>
<div class="viewcode-block" id="ThreadedFunc.quit"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ThreadedFunc.quit">[docs]</a>  <span class="k">def</span> <span class="nf">quit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; A method to check if the &quot;we want abort&quot; flag is set from inside</span>
<span class="sd">        the threaded function.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="bp">True</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_quit</span> <span class="k">else</span> <span class="bp">False</span>
</div>
<div class="viewcode-block" id="ThreadedFunc.run"><a class="viewcode-back" href="../../ccqw.html#ccqw.progressDialog.ThreadedFunc.run">[docs]</a>  <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Start the Thread</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">returnValue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_func</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">progressSignal</span><span class="o">.</span><span class="n">emit</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">quit</span><span class="p">,</span>
      <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_args</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">_kargs</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">endedSignal</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">returnValue</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">closeSignal</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>

<span class="c">#===============================================================================</span></div></div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>

  <span class="kn">import</span> <span class="nn">signal</span>
  <span class="kn">import</span> <span class="nn">time</span>
  <span class="n">signal</span><span class="o">.</span><span class="n">signal</span><span class="p">(</span><span class="n">signal</span><span class="o">.</span><span class="n">SIGINT</span><span class="p">,</span> <span class="n">signal</span><span class="o">.</span><span class="n">SIG_DFL</span><span class="p">)</span>

  <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">progress</span><span class="p">,</span> <span class="n">qfunc</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kargs</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">51</span><span class="p">):</span>
      <span class="k">if</span> <span class="n">qfunc</span><span class="p">():</span>
        <span class="k">return</span> <span class="bp">None</span>
      <span class="n">v</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">i</span>
      <span class="k">print</span> <span class="s">&quot;arg=&quot;</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">v</span>
      <span class="n">progress</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
      <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
    <span class="k">return</span> <span class="s">&quot;Something&quot;</span>

  <span class="n">app</span> <span class="o">=</span> <span class="n">QtGui</span><span class="o">.</span><span class="n">QApplication</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">)</span>
  <span class="n">tf</span> <span class="o">=</span> <span class="n">ThreadedFunc</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s">&#39;pippo&#39;</span><span class="p">,</span> <span class="s">&#39;pluto&#39;</span><span class="p">)</span>
  <span class="n">pd</span> <span class="o">=</span> <span class="n">ProgressDialog</span><span class="p">(</span><span class="n">tf</span><span class="p">)</span>
  <span class="k">print</span> <span class="s">&quot;Progress dialog ended, execution can continue.....&quot;</span>
  <span class="n">app</span><span class="o">.</span><span class="n">exec_</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/ogs.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Roberto Vidmar, Nicola Creati.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>